// //公共二级弹层
// (function () {
//     var io = ppt.io;
//     function Bag () {
//         Bag.super(this);
//         this.width     = 1334;
//         this.height    = Laya.stage.height;

       
//         this.init();
//     }
//     //继承
//     Laya.class(Bag, "Alert.Bag", Laya.Sprite);

//     var _proto = Bag.prototype;
//     _proto.destroy = function () {
       
//         Laya.Sprite.prototype.destroy.call(this, true);
        
//     }
//     //初始化
//     _proto.init = function () {
//         var bag = new ui.Alert.BagLayerUI();
//         this.addChild(bag);
//         bag.confirmBtn.on(Laya.Event.CLICK, this, this.layerNext);
//         bag.reductionBtn.on(Laya.Event.CLICK, this, this.Reduction);
//         bag.addBtn.on(Laya.Event.CLICK, this, this.Add);
//         bag.maxBtn.on(Laya.Event.CLICK, this, this.Max);
//         bag.cancelBtn.on(Laya.Event.CLICK, this, this.Close);
//         bag.getChildByName('close').on(Laya.Event.CLICK, this, this.Close);
// ;    }
// })();


(function (_super) {
    var CURRENCY_ICON_INDEX = {
        "1" : 0,
        "2" : 1,
        "3" : 2
    };
    var CURRENCY_NAME = {
        "1" : "金币",
        "2" : "钻石",
        "3" : "勋章"
    };
    var CURRENCY_TYPE = {
        "1" : "coin",
        "2" : "diamond",
        "3" : "honor"
    };
    var EVENT_CLICK = Laya.Event.CLICK;
    var io = ppt.io;

    function Purchase(data) {
        Purchase.super(this);

        this.purchaseNum = 0;
        this.maxItemNum = 0;
        this.unitPrice = 0
        this.maxBuyItem = 0;
        this.userMoney = 0;
        this.itemId = null;
        this.itemName = null;
        this.currencyType = null;
        this.data = null;

        this.init(data);
    }
    Laya.class(Purchase, "Alert.Purchase", _super);
    var _proto = Purchase.prototype;
    _proto.destroy = function () {
        _super.prototype.destroy.call(this, true);

        io.unregister("game::enter", this.updateMaxBuyNum);

        this.purchaseNum = null;
        this.maxItemNum = null;
        this.unitPrice = null
        this.maxBuyItem = null;
        this.userMoney = null;
        this.itemId = null;
        this.itemName = null;
        this.currencyType = null;
        this.data = null;
    }

    _proto.init = function (data) {
        this.callLater(function () {
            io.register("game::enter", this, this.updateMaxBuyNum);
            io.emit({"cmd" : "game::enter"});
        });
        this.data = data;

        this.bindEvent();
        this.update(data);
    }
    _proto.bindEvent = function () {
        this.reductionBtn.on(EVENT_CLICK, this, this.minus);
        this.addBtn.on(EVENT_CLICK, this, this.plus);
        this.btnMax.on(EVENT_CLICK, this, this.max);
        this.buyBtn.on(EVENT_CLICK, this, this.purchase);
        this.btnClose.on(EVENT_CLICK, this, this.destroy);
    }
    _proto.update = function (data) {
        console.log("Alert Purchase Update", data);
        this.titleName.text = data.name;
        this.goodsName.text = data.name;
        this.goodsPrice.text = data.price;
        this.goodsNum.text = data.purchaseNum + "个";
        this.content.text = data.desc;
        this.picall.skin = PROPS_PATH + data.item_id + ".png";
        this.picMoney.index = CURRENCY_ICON_INDEX[data.currency];
        this.picMoneyall.index = CURRENCY_ICON_INDEX[data.currency];

        this.itemName = data.name;
        this.purchaseNum = data.purchaseNum;
        this.maxItemNum = data.num;
        this.unitPrice = data.price;
        this.itemId = data.item_id;
        this.currencyType = data.currency;
    }
    _proto.updateMaxBuyNum = function (data) {
        this.maxBuyItem = Math.min(this.maxItemNum, data[CURRENCY_TYPE[this.currencyType]] / this.unitPrice | 0);
        this.maxBuyItem = this.maxBuyItem < 1 ? 1 : this.maxBuyItem;

        this.calc();
    }
    _proto.minus = function () {
        this.purchaseNum--;
        if(this.purchaseNum <= 1){
            this.purchaseNum = 1;
        }
        this.calc();
    }
    _proto.plus = function () {
        this.purchaseNum++;
        if(this.purchaseNum >= this.maxBuyItem){
            this.purchaseNum = this.maxBuyItem;
        }
        this.calc();
    }
    _proto.max = function () {
        this.purchaseNum = this.maxBuyItem;
        this.calc();
    }
    _proto.calc = function () {
        this.chooseNum.text = this.purchaseNum;
        this.allPrice.text = this.purchaseNum * this.unitPrice;
    }
    _proto.purchase = function () {
        //io.emit({'cmd' : 'shop::buy', params : {'currency' : this.currencyType, 'itemId' : this.itemId, 'count' : this.purchaseNum}});
        var config = {
            msg : "您将花费" + this.allPrice.text + CURRENCY_NAME[this.currencyType] + "购买" + this.purchaseNum + "个" + this.itemName,
            btn : {
                btnCancel : {
                    skin : "cancel"
                },
                btnConfirm : {
                    skin : "confirm",
                    click : function (currencyType, itemId, purchaseNum) {
                        ppt.io.emit({'cmd' : 'shop::buy', params : {'currency' : currencyType, 'itemId' : itemId, 'count' : purchaseNum}});
                    }.bind(null, this.currencyType, this.itemId, this.purchaseNum)
                }
            },
            close : true
        }
        ppt.director.popScene(new Alert.Public(config));
    }
})(ui.Alert.SellLayerUI);